import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;



public class Logger {
	public static Connection connection = null;  
	public static ResultSet resultSet = null;  
	public static Statement statement = null;
	public static final String DATE_FORMAT_NOW = "HH yyyy-MM-jj: mm: ss";
	
	public Logger(){
		try {
			Class.forName("org.h2.Driver");  
			connection = DriverManager.getConnection("jdbc:h2:./system/DBsys/DBSys", "admin", "admin");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void log(String message){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		message = dateFormat.format(cal.getTime()) + " : " +  message; 
		String log = "INSERT INTO log " + "VALUES " + "('" + message +"')";
		try {
			statement = connection.createStatement();
			ResultSet r = null;
			statement.execute(log);
			r.close();
			statement.close();
			connection.close();
		}
		catch(Exception e){
			
		}
		
		String fileLog = "./log/robotLog.log";
		try {
			FileWriter fw = new FileWriter(fileLog, true);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(message +"\n");
			output.flush();
			output.close();
		}
		catch (Exception e){
			System.out.println("[ERREUR] Logger fileLog");
		}
	}
	
	public void exit() {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
